<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Phongmay extends MY_Controller{
    var $submneu = array();
    function __construct(){
        parent::__construct();
        $this->_data['mainmenu'] = 'pm';
        $this->_data['trangthaitb'] = array();
        $this->load->helper('form');
        $this->submenu();
        //$this->submneu[] = anchor(site_url('phongmay'),'Phòng máy','class=>');
    }
    
    function submenu($pos=1){
        
        $this->_data['submenu'] = array(anchor(site_url('phongmay'),'Danh sách máy tính',array('class'=>'first'.($pos==1?' selected':''))),anchor(site_url('phongmay/sua-may-tinh'),'Sửa máy',array('class'=>($pos==2?' selected':''))));
    }
    
    function _trangthaitb(){
        $list_trangthai = $this->db->get('trangthai')->result();
        if(!empty($list_trangthai)){
            foreach($list_trangthai as $tt){
                $this->_data['trangthaitb'][$tt->tt_id]= $tt->tt_name;
            }
        }
    }
    
    function _phongmay(){
        $list_trangthai = $this->db->get('pm')->result();
        if(!empty($list_trangthai)){
            foreach($list_trangthai as $tt){
                $this->_data['listpm'][$tt->pm_id]= $tt->pm_name;
            }
        }
    }
    
    function _loailkmt(){        
        $list_llk = $this->db->get('loailinhkien')->result();
        if(!empty($list_llk)){
            foreach($list_llk as $tt){
                $this->_data['listllkmt'][$tt->llk_id]= $tt->llk_name;
            }
        }
    }
    
    function _linhkienmaytinh(){        
        /*$this->db->select('ctmt.*,linhkien_maytinh.ten as tenlinhkien,dungluong,id_llk');
        $this->db->from('linhkien_maytinh');
        $this->db->join('ctmt','ctmt.id_lkmt=linhkien_maytinh.id','join');
        $list_ = $this->db->get()->result();*/
        $list_ = $this->db->get('linhkien_maytinh')->result();
        if(!empty($list_)){
            foreach($list_ as $tt){
                $this->_data['listltmt'][$tt->id_llk][$tt->id]= $tt;
            }
        }
    }
    
    public function index($id=1){
        if(empty($id) && !is_numeric($id)) redirect('');
        if(!empty($_POST['pm'])) $id=$_POST['pm'];
        
        $this->_phongmay();//$this->_data['listpm'] = $this->db->get('pm')->result();
        $this->_loailkmt();
        $this->_linhkienmaytinh();
        $this->_trangthaitb();
        
        $this->db->select('maytinh.ten,ctmt.*,linhkien_maytinh.ten as tenlinhkien,dungluong,id_llk');
        $this->db->from('maytinh');        
        $this->db->join('ctmt','ctmt.id_mt=maytinh.id','join');
        $this->db->join('linhkien_maytinh','ctmt.id_lkmt=linhkien_maytinh.id','join');
        $listmt = $this->db->get()->result();
        $listmt_new = array();
        if(!empty($listmt)){
            
            foreach($listmt as $mt){                
                $listmt_new[$mt->id_mt]['ten'] = $mt->ten;
                $listmt_new[$mt->id_mt][$mt->id_llk][] = $mt->tenlinhkien.' '.$mt->dungluong. '('.$mt->soluong.')';//array('tenlinhkien'=>$mt->tenlinhkien.' '.$mt->dungluong,'slsudung'=>$mt->soluong);
            }
        }
        //echo '<pre>';
        //var_dump($listmt_new);
        $this->_data['listmt'] = $listmt_new;
        
        $this->_data['listtbpm'] = $this->db->get_where('thietbi',array('pm_id'=>$id))->result();//lay tat ca cac thiet bi cua phong may
        $this->_data['listlkpm'] = $this->db->get_where('linhkien',array('pm_id'=>$id))->result();//lay tat ca cac linh kien cua phong may
        
        
        $this->_load_view('phongmay/content');
    }
    
    function suamt($id){
        if(empty($id) || !is_numeric($id)) redirect('phongmay');
        else{
            $this->_loailkmt();
            $this->_phongmay();
            $this->_linhkienmaytinh();
            /*$output = array(
                'title' => 'Sửa máy tính',
                'html' => ''
            );*/
            $this->db->select('maytinh.ten,maytinh.pm_id,ctmt.*,id_llk');
            $this->db->from('maytinh');        
            $this->db->join('ctmt','ctmt.id_mt=maytinh.id','join');
            $this->db->join('linhkien_maytinh','ctmt.id_lkmt=linhkien_maytinh.id','join');
            $this->db->where(array('maytinh.id'=>$id));
            $listmt = $this->db->get()->result();
            $listmt_new = array();
            if(!empty($listmt)){            
                foreach($listmt as $mt){                
                    $_POST['pm_id'] = $mt->pm_id;
                    $_POST['tenmt'] = $mt->ten;                
                    $listmt_new[$mt->id_llk][] = $mt;
                }
            }
            $this->_data['listlkmt'] = $listmt_new;
            $this->_load_view('phongmay/suaphongmay');
        }        
    }
    
}